#pragma once

#include<vector>

template<class K>
struct HashFunc
{
	size_t operator()(const K& key)
	{
		return key;
	}
};

template<>
struct HashFunc<string>
{
	size_t operator()(const string& key)
	{
		size_t hash = 0;
		for (auto e : key)
		{
			hash += e;
			hash *= 31;
		}
		return hash;
	}
};


namespace HashBucket
{

	template<class T>
	struct HashNode
	{
		HashNode* _next;
		T _data;

		HashNode(const T& data)
			:_next(nullptr)
			, _data(data)
		{}
	};

	template<class K, class T, class KeyOfT, class Hash>
	class HashTable;

	template<class K,class T, class Ref, class Ptr,class KeyOfT,class Hash>
	struct __HashIterator
	{
		typedef HashNode<T> Node;
		typedef HashTable<K, T, KeyOfT, Hash> HT;

		typedef __HashIterator<K, T, T&, T*, KeyOfT, Hash> iterator;
		typedef __HashIterator<K, T, Ref, Ptr, KeyOfT, Hash> Self;


		__HashIterator(const iterator& it)
			:_node(it._node), _ht(it._ht)
		{}
		
		__HashIterator(Node* node,const HT* ht)
			:_node(node), _ht(ht)
		{}

		Ref operator*()
		{
			return _node->_data;
		}

		Ptr operator->()
		{
			return &(_node->_data);
		}

		bool operator!=(const Self& s)
		{
			return _node != s._node;
		}

		bool operator==(const Self& s)
		{
			return !(*this != s);
		}

		Self& operator++()
		{
			
			if (_node->_next)
				_node = _node->_next;
			else
			{
				KeyOfT kot;
				size_t hashi = Hash()(kot(_node->_data)) % _ht->_tables.size();
				++hashi;
				while(hashi< _ht->_tables.size())
				{
					if (_ht->_tables[hashi])
					{
						_node = _ht->_tables[hashi];
						break;
					}
					else
					{
						++hashi;
					}
				}
				if(hashi >= _ht->_tables.size())
				{
					_node = nullptr;
				}
			}
			return *this;
		}

		Node* _node;
		const HT* _ht;
	};

template<class K, class T, class KeyOfT, class Hash>
	class HashTable
	{
		template<class K, class T, class Ref, class Ptr, class KeyOfT, class Hash>
		friend struct __HashIterator;

		typedef HashNode<T> Node;
	public:
		typedef __HashIterator<K, T, T&, T*, KeyOfT, Hash> iterator;
		typedef __HashIterator<K, T, const T&, const T*, KeyOfT, Hash> const_iterator;

		iterator begin()
		{
			for (size_t i = 0; i < _tables.size(); ++i)
			{
				if (_tables[i])
					return iterator(_tables[i], this);
			}
			return iterator(nullptr, this);	
		}
		
		iterator end()
		{
			return iterator(nullptr, this);
		}


		const_iterator begin() const
		{
			for (size_t i = 0; i < _tables.size(); ++i)
			{
				if (_tables[i])
					return iterator(_tables[i], this);
			}
			return const_iterator(nullptr, this);
		}


		const_iterator end() const
		{
			return const_iterator(nullptr, this);
		}

		~HashTable()
		{
			for (auto& cur : _tables)
			{
				while (cur)
				{
					Node* next = cur->_next;
					delete cur;
					cur = next;
				}
				cur = nullptr;
			}
		}



		size_t GetNextPrime(size_t prime)
		{
			// SGI
			static const int __stl_num_primes = 28;
			static const unsigned long __stl_prime_list[__stl_num_primes] =
			{
				53, 97, 193, 389, 769,
				1543, 3079, 6151, 12289, 24593,
				49157, 98317, 196613, 393241, 786433,
				1572869, 3145739, 6291469, 12582917, 25165843,
				50331653, 100663319, 201326611, 402653189, 805306457,
				1610612741, 3221225473, 4294967291
			};

			size_t i = 0;
			for (; i < __stl_num_primes; ++i)
			{
				if (__stl_prime_list[i] > prime)
					return __stl_prime_list[i];
			}

			return __stl_prime_list[i];
		}
		pair<iterator,bool> Insert(const T& data)
		{
			KeyOfT kot;
			iterator it = Find(data);
			if (it != end())
				return make_pair(it,false);


			if (_n == _tables.size())
			{
				/*size_t newsize = _n == 0 ? 10 : _tables.size() * 2;*/
				size_t newsize = GetNextPrime(_tables.size());
				vector<Node*> newtables(newsize, nullptr);

				for (auto& cur : _tables)
				{
					while (cur)
					{
						Node* next = cur->_next;
						size_t hashi = Hash()(kot(cur->_data)) % newsize;

						cur->_next = newtables[hashi];
						newtables[hashi] = cur;

						cur = next;
					}
					cur = nullptr;
				}
				_tables.swap(newtables);
			}

			size_t hashi = Hash()(kot(data)) % _tables.size();

			Node* newNode = new Node(data);
			newNode->_next = _tables[hashi];
			_tables[hashi] = newNode;
			++_n;
			return make_pair(iterator(newNode,this),true);
		}

		iterator Find(const T& data)
		{
			KeyOfT kot;
			if (_tables.size() == 0)
				return iterator(nullptr, this);
			size_t hashi = Hash()(kot(data)) % _tables.size();

			Node* cur = _tables[hashi];
			while (cur)
			{
				if (kot(cur->_data) == kot(data))
					return iterator(cur,this);
				cur = cur->_next;
			}
			return iterator(nullptr, this);
		}

		bool Erase(const T& data)
		{
			size_t hashi = Hash()(kot(data)) % _tables.size();

			Node* prev = nullptr;
			Node* cur = _tables[hashi];
			while (cur)
			{
				if (kot(cur->_data) == kot(data))
				{
					if (prev)
					{
						prev->_next = cur->_next;
					}
					else
					{
						_tables[hashi] = cur->_next;
					}
					delete cur;
					return true;
				}
				else
				{
					prev = cur;
					cur = cur->_next;
				}
			}
			return false;
		}
	private:
		vector<Node*> _tables;
		size_t _n = 0;
	};
}




